<?php

namespace application\Module;

use library\Core\Db\CDbCriteria;
use application\Model\FlowLogModel;
use library\Core\Singleton;

class FlowLog extends Singleton
{
    public function getCountByPhone($phone = '')
    {
        $criteria = new CDbCriteria();
        $criteria->select(['number' => '(count(*))']);
        $criteria->addEqCondition('phone', $phone);
        $criteria->addEqCondition('code', 0);
        $model = new FlowLogModel();

        return $model->getOne($criteria);
    }

    /**
     * @param $data
     * @return string
     * 插入日志信息
     */
    public function insertData($data)
    {
        $criteria = new CDbCriteria();
        $criteria->insert($data);
        $model = new FlowLogModel();

        return $model->add($criteria);
    }

    /**
     * @return array
     * 获取当天剩余名额
     */
    public function getDayCount()
    {
        $criteria = new CDbCriteria();
        $criteria->select(['number' => '(count(*))']);
        $time_start = strtotime(date('Y-m-d', time()));
        $time_end = $time_start + 24 * 3600;
        $criteria->addBetweenCondition('create_time', $time_start, $time_end);
        $model = new FlowLogModel();
        $total = 88;
        $used = $model->getOne($criteria);
        return $total > $used ? $total - $used :0;
    }
}
